package com.atguigu.educenter.controller;

import com.atguigu.commonutils.MD5;
import com.atguigu.commonutils.R;
import com.atguigu.educenter.entity.MemberInfoObj;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * @Author: GengKY
 * @Date: 2022/4/18 8:58
 */
@Api(description = "用户管理")
@RestController
@RequestMapping("/educenter/memberinfo")
public class MemberController {

    @Autowired
    UcenterMemberService memberService;

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @ApiOperation("根据用户ID修改头像，从Redis拿")
    @PostMapping("/changeAvatarById3/{memberid}")
    public R changeAvatarById3(@PathVariable("memberid")String memberid){
        System.out.println("根据用户ID修改头像3");
        UcenterMember member = memberService.getById(memberid);
        String useravatar= redisTemplate.opsForValue().get("MemberAvatar").toString();
        member.setAvatar(useravatar);
        memberService.updateById(member);
        return R.ok();
    }

    @ApiOperation("根据用户ID修改头像2")
    @PostMapping("/changeAvatarById2/{memberid}")
    public R changeAvatarById2(@PathVariable("memberid")String memberid, @RequestBody MemberInfoObj memberInfoObj){
        System.out.println("根据用户ID修改头像2");
        System.out.println("MemberInfoObj:"+memberInfoObj.toString());
        UcenterMember member = memberService.getById(memberid);
        member.setAvatar(memberInfoObj.getAvatar());
        memberService.updateById(member);
        return R.ok();
    }

    @ApiOperation("根据用户ID修改头像1")
    @PostMapping("/changeAvatarById1/{memberid}/{avatarpath}")
    public R changeUserAvatarById(@PathVariable("memberid")String memberid,@PathVariable("avatarpath") String avatarpath){
        System.out.println("根据用户ID修改头1");
        UcenterMember member = memberService.getById(memberid);
        member.setAvatar(avatarpath);
        memberService.updateById(member);
        return R.ok();
    }


    @ApiOperation("根据用户ID修改用户名")
    @PostMapping("/changeMemberName/{memberid}/{memberName}")
    public R changeUserNameById(@PathVariable("memberid")String memberid,
                                @PathVariable("memberName")String memberName){
        UcenterMember member = memberService.getById(memberid);
        member.setNickname(memberName);
        memberService.updateById(member);
        return R.ok();
    }


    @ApiOperation("根据用户ID修改用户年龄")
    @PostMapping("/changeMemberAge/{memberid}/{memberAge}")
    public R changeUserAgeById(@PathVariable("memberid")String memberid,
                                @PathVariable("memberAge")String memberAge){
        UcenterMember member = memberService.getById(memberid);
        member.setAge(Integer.valueOf(memberAge));
        memberService.updateById(member);
        return R.ok();
    }

    @ApiOperation("根据用户ID修改用户签名")
    @PostMapping("/changeMemberSign/{memberid}/{memberSign}")
    public R changeUserSignById(@PathVariable("memberid")String memberid,
                                @PathVariable("memberSign")String memberSign){
        UcenterMember member = memberService.getById(memberid);
        member.setSign(memberSign);
        memberService.updateById(member);
        return R.ok();
    }


    @ApiOperation("根据用户ID修改手机")
    @PostMapping("/changeMemberPhone/{memberid}/{memberPhone}")
    public R changeUserPhoneById(@PathVariable("memberid")String memberid,
                                @PathVariable("memberPhone")String memberPhone){
        UcenterMember member = memberService.getById(memberid);
        member.setMobile(memberPhone);
        memberService.updateById(member);
        return R.ok();
    }

    @ApiOperation("根据用户ID修改密码")
    @PostMapping("/changeMemberPass/{memberid}/{memberPass}")
    public R changeUserPassById(@PathVariable("memberid")String memberid,
                                @PathVariable("memberPass")String memberPass){
        UcenterMember member = memberService.getById(memberid);
        String md5password = MD5.encrypt(memberPass);
        member.setPassword(md5password);
        memberService.updateById(member);
        return R.ok();
    }



}
